/**
 * 初始化 首页
 */
layui.define(['base','form'], function (exports) {
    var $=layui.jquery,layer=layui.layer,$setting=layui.config,$http=layui.http,$global=layui.common,form = layui.form(),
    request = $http.get($setting.config.user.info);request.async = false;
    var filename,layerIndex //loading索引;


        // 日期插件
    $('#dateTime').daterangepicker({
        format: 'YYYY-MM-DD ',
        endDate: moment(),
        maxDate: moment()
    }, function(start, end, label) {});
    $("#dateTime").on('apply.daterangepicker', function(ev, picker) {
        start_time = picker.startDate.format('YYYY-MM-DD HH:mm:ss');
        end_time = picker.endDate.format('YYYY-MM-DD HH:mm:ss');
    });

    // 文件上传组件上传框
    var projectfileoptions = {
        showUpload: false,
        showRemove: true,
        browseClass: "btn btn-success",
        showPreview: true,
        allowedPreviewTypes: ['image'],
        browseLabel: "浏览",
        removeLabel: "移除",
        uploadLabel: "上传",
        // allowedFileExtensions: ['jpg', 'png', 'gif'],
        msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
        maxFileSize: 10000
    };
    // 文件上传组件加载
    $('input[class=file-loading]').each(function() {
        $(this).fileinput(projectfileoptions);
    });
    // 选中文件之后
    $('#file').on('filebatchselected', function(event, files) {
        filename = files[0].name;
        $('#filename').val(filename);
    });

    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
    var ue = UE.getEditor('editor');
    // 初始化
    var s = '<p>chrome浏览器下，单图上传有问题！IE没问题！<img src="http://img.baidu.com/hi/jx2/j_0060.gif"/><img src="http://img.baidu.com/hi/jx2/j_0002.gif"/><img src="http://img.baidu.com/hi/jx2/j_0068.gif"/><img src="http://img.baidu.com/hi/jx2/j_0059.gif"/></p>';
    ue.ready(function() {//编辑器初始化完成再赋值
        ue.setContent(s);  //赋值给UEditor
    });



    // 搜索
    $('#search').click(function() {
        // $table.bootstrapTable('refresh', { silent: true });
    });
    // 重置
    $('#reset').click(function() {
        start_time = null;
        end_time = null;
    });

    form.on('submit(*)', function(data){
        layerIndex = layer.load();
        var fdata = data.field;
        var formData = new FormData();
        for (var key in fdata){
            formData.append(key,fdata[key]);
        }
        formData.append('file', $('#file')[0].files[0]);
        $.ajax({
            url: $setting.getUri()+$setting.config.demo.upload,
            type: 'POST',
            cache: false,
            data: formData,
            dataType:'json',
            processData: false,
            contentType: false,
            crossDomain: true,
            xhrFields: {
                withCredentials: true
            }
        }).done(function(obj) {
            layer.close(layerIndex);
            if (obj.code == '0') {
                layer.msg("提交成功！", { icon: 1 });
                $('form').resetForm();
                $('#timeDisplay').hide();
                $('#search').click();
            } else {
                layer.msg("提交失败：" + obj.message, { icon: 2 });
            }
        });
    });

    // 提交
    $('#publish').click(function(){
        $http.post('/index/publish', {'content':ue.getContent()}).send(function (obj) {
            if (obj.code != '0') {
                layer.msg(obj.message, {icon: 2});
            }
        });
    });
    exports('../app/demo', {});

});